//
//  SecurityPwdUIView.m
//  EquesCat
//
//  Created by Chun Hung Cheung on 2025/3/29.
//

#import "SecurityPwdView.h"
#import "HWTFCodeBView.h"
@interface SecurityPwdView()<UITextFieldDelegate>
{
    UIView *bgView;
    BOOL isH5Device;
    UITextField *_pasTextfield1;
}
@end

@implementation SecurityPwdView



- (instancetype)initWithSetSercurityPsd:(NSString*)title andSubTitle:(NSString*)subTitle iconName:(NSString*)iconName type:(int)type deviceBid:(NSString*)deviceBid isView:(BOOL)isView
{
    if (self = [super init]) {
        [self setpsdView:title subTitle:subTitle iconName:iconName type:type deviceBid:deviceBid isView:isView];
    }
    return self;
}

- (void)setpsdView:(NSString*)title subTitle:(NSString*)subTitle iconName:(NSString*)iconName type:(int)type deviceBid:(NSString*)deviceBid isView:(BOOL)isView
{
     self.frame = [UIScreen mainScreen].bounds;
     self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3/1.0];
    //backgroundView
    [bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
     bgView = [[UIView alloc]init];
    [self addSubview:bgView];
     bgView.backgroundColor = [UIColor clearColor];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (isView) {
            make.centerY.equalTo(self.mas_centerY).offset(-[UIDevice dev_navigationBarHeight]);
        }else{
            make.centerY.equalTo(self.mas_centerY);
        }
        make.centerX.equalTo(self.mas_centerX);
        if (type == 2) {
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(260);
        }else{
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(300);
        }
    }];
    
    
   UIView *whiteview = [[UIView alloc]init];
   whiteview.backgroundColor = [UIColor whiteColor];
   whiteview.layer.masksToBounds = YES;
   whiteview.layer.cornerRadius = 18.0f;
   [bgView addSubview:whiteview];
   [whiteview mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(bgView.mas_top).offset(2);
       if (type == 2) {
           make.width.mas_equalTo(300);
           make.height.mas_equalTo(260);
       }else{
           make.width.mas_equalTo(300);
           make.height.mas_equalTo(300);
       }
       make.centerX.equalTo(self.mas_centerX);
   }];
    

    if (iconName.length > 0) {
        UIImageView *showimage = [[UIImageView alloc]init];
         showimage.image = [UIImage imageNamed:iconName];
         showimage.contentMode = UIViewContentModeCenter;
        [bgView addSubview:showimage];
        [showimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView.mas_top).offset(-5);
            make.left.equalTo(bgView.mas_left);
            make.right.equalTo(bgView.mas_right);
            make.height.mas_equalTo(113);
        }];
        
        UILabel *topLabel = [[UILabel alloc]init];
        topLabel.text = title;
        topLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
        topLabel.textColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
       [topLabel sizeToFit];
        topLabel.numberOfLines = 0;
       [showimage addSubview:topLabel];
       [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(showimage.mas_centerX);
            make.centerY.equalTo(showimage.mas_centerY);
       }];
    }else{
        UILabel *topLabel = [[UILabel alloc]init];
        topLabel.text = title;
        topLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
        topLabel.textColor = [HexChangeRGB colorWithHexString:@"#FF7316"];
       [topLabel sizeToFit];
        topLabel.numberOfLines = 0;
       [whiteview addSubview:topLabel];
       [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(whiteview.mas_centerX);
            make.top.equalTo(whiteview.mas_top).offset(30);
       }];
    }
    
    if (type == 2) {
        UILabel *subLabel = [[UILabel alloc]init];
        subLabel.text = subTitle;
        subLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        subLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
       [subLabel sizeToFit];
        subLabel.numberOfLines = 0;
       [whiteview addSubview:subLabel];
       [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(whiteview.mas_centerX);
            make.top.equalTo(whiteview.mas_top).offset(95);
       }];
        
        HWTFCodeBView *codeview = [[HWTFCodeBView alloc]initWithCount:6 margin:8];
        codeview.type = type;
        codeview.ClickBlock = ^(int type, NSString * _Nonnull text) {
            NSLog(@"text-->%@",text);
            if (self.delegate && [self.delegate respondsToSelector:@selector(sercurityCode:type:)]) {
                [self.delegate sercurityCode:text type:type];
            }
        };
        [whiteview addSubview:codeview];
        [codeview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(50);
            make.top.equalTo(subLabel.mas_bottom).offset(20);
            make.left.equalTo(whiteview.mas_left);
        }];
    }else{
        UILabel *subLabel = [[UILabel alloc]init];
        subLabel.text = subTitle;
        subLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        subLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
       [subLabel sizeToFit];
        subLabel.numberOfLines = 0;
       [whiteview addSubview:subLabel];
       [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(whiteview.mas_centerX);
            make.top.equalTo(whiteview.mas_top).offset(135);
       }];
        
        HWTFCodeBView *codeview = [[HWTFCodeBView alloc]initWithCount:6 margin:8];
        codeview.type = type;
        codeview.ClickBlock = ^(int type, NSString * _Nonnull text) {
            NSLog(@"text-->%@",text);
            [self dismiss];
            if (self.delegate && [self.delegate respondsToSelector:@selector(sercurityCode:type:)]) {
                [self.delegate sercurityCode:text type:type];
            }
        };
        [whiteview addSubview:codeview];
        [codeview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(50);
            make.top.equalTo(subLabel.mas_bottom).offset(20);
            make.left.equalTo(whiteview.mas_left);
        }];
    }
    
    if (type == 2) {
        UIButton *disBtn = [[UIButton alloc]init];
       [disBtn setImage:[UIImage imageNamed:@"vl0_top_popup_close"] forState:UIControlStateNormal];
       [disBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
       [whiteview addSubview:disBtn];
       [disBtn mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(whiteview.mas_top);
           make.right.equalTo(whiteview.mas_right);
           make.height.mas_equalTo(30);
           make.width.mas_equalTo(30);
       }];
    }
}

- (void)dismiss
{
      [UIView animateWithDuration:0.6 animations:^{
          self.transform = (CGAffineTransformMakeScale(1.5, 1.5));
          self.backgroundColor = [UIColor clearColor];
          self.alpha = 0;
      }completion:^(BOOL finished) {
          [self removeFromSuperview];
      }];
      
      if (self.delegate && [self.delegate respondsToSelector:@selector(dissMiss)]) {
          [self.delegate dissMiss];
      }
      
}


- (void)show
{
    [self showWithAlert:bgView];
}


/**
添加Alert入场动画
@param alert 添加动画的View
*/
- (void)showWithAlert:(UIView*)alert{

CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.6;

    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
     animation.values = values;
    [alert.layer addAnimation:animation forKey:nil];
}

@end
